package com.hr.borrowingLog.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.service.IBorrowingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hr.common.annotation.Log;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.enums.BusinessType;
import com.hr.borrowingLog.domain.BorrowingLog;
import com.hr.borrowingLog.service.IBorrowingLogService;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * 查询借支记录Controller
 * 
 * @author guan
 * @date 2024-07-20
 */
@RestController
@RequestMapping("/manage/borrowingLog")
public class BorrowingLogController extends BaseController
{
    @Autowired
    private IBorrowingLogService borrowingLogService;

    @Resource
    private IBorrowingService borrowingService;

    /**
     * 查询查询借支记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(BorrowingLog borrowingLog)
    {
        startPage();
        List<BorrowingLog> list = borrowingLogService.selectBorrowingLogList(borrowingLog);
        return getDataTable(list);
    }

    /**
     * 用户查询与自身相关借支信息
     */
//    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:associationList')")
    @GetMapping("/associationList")
    public TableDataInfo associationList(BorrowingLog borrowingLog)
    {
        startPage();
        borrowingLog.setUserId(getLoginUser().getUserId());
        List<BorrowingLog> list = borrowingLogService.selectBorrowingLogAssociationList(borrowingLog);
        return getDataTable(list);
    }

    /**
     * 导出查询借支记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:export')")
    @Log(title = "查询借支记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BorrowingLog borrowingLog)
    {
        List<BorrowingLog> list = borrowingLogService.selectBorrowingLogList(borrowingLog);
        ExcelUtil<BorrowingLog> util = new ExcelUtil<BorrowingLog>(BorrowingLog.class);
        util.exportExcel(response, list, "查询借支记录数据");
    }

    /**
     * 获取查询借支记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:query')")
    @GetMapping(value = "/{borrowingLogId}")
    public AjaxResult getInfo(@PathVariable("borrowingLogId") Long borrowingLogId)
    {
        return success(borrowingLogService.selectBorrowingLogByBorrowingLogId(borrowingLogId));
    }

    /**
     * 新增查询借支记录
     */
//    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:add')")
    @Log(title = "查询借支记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BorrowingLog borrowingLog)
    {
        Borrowing borrowing = borrowingService.selectBorrowingById(borrowingLog.getUserId());
        Integer amount = borrowingLog.getborrowingLogAmount();
        if (borrowing.getNumber() == borrowing.getBorrowedNumber())
            return error("借款失败，当月借支次数已耗尽");
        if (borrowing.getBorrowedNumber() + amount > borrowing.getAmount())
            return error("借款失败，数额已超出范围");
        return toAjax(borrowingLogService.insertBorrowingLog(borrowingLog));
    }

    /**
     * 修改查询借支记录
     */
//    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:edit')")
    @Log(title = "查询借支记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BorrowingLog borrowingLog)
    {
        if (borrowingLog.getUpdateTime() != null)
            return error("修改失败，不可重复修改");
        return toAjax(borrowingLogService.updateBorrowingLog(borrowingLog));
    }

    /**
     * 删除查询借支记录
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:remove')")
    @Log(title = "查询借支记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{borrowingLogIds}")
    public AjaxResult remove(@PathVariable Long[] borrowingLogIds)
    {
        return toAjax(borrowingLogService.deleteBorrowingLogByBorrowingLogIds(borrowingLogIds));
    }

    /**
     * 统计金额
     * @param borrowingLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowingLog:countAmount')")
    @GetMapping("/countAmount")
    public AjaxResult countAmount(BorrowingLog borrowingLog){
        return success(borrowingLogService.countAmount(borrowingLog));
    }

}
